package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechatService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service

public class WechatServiceImpl implements WechatService {
    // 登录
    private static final String REQUEST_URL = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code";

    // 获取token
    private static final String TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    // 获取手机号
    private static final String PHONE_REQUEST_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
    private static final Logger log = LoggerFactory.getLogger(WechatServiceImpl.class);

    @Value("${wechat.appId}")
    private String appId;
    @Value("${wechat.appSecret}")
    private String appSecret;
    @Override
    public String  getOpenid(String code) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appId);
        paramMap.put("secret", appSecret);
        paramMap.put("js_code", code);
        //调用微信登录接口
        String json= HttpUtil.get(REQUEST_URL, paramMap);
        JSONObject jsonObject = JSONUtil.parseObj(json);
        if (ObjectUtil.isNotNull(jsonObject.getInt("errcode"))){
            throw new RuntimeException("获取openId失败");
        }

        return jsonObject.getStr("openid");
    }

    @Override
    public String getPhone(String code) {
        //获取token
        String token = getToken();
        //通过 token获取手机号
        Map<String, Object> param = new HashMap<>();
        param.put("code",code);
        String json = HttpUtil.post(PHONE_REQUEST_URL + token, JSONUtil.toJsonStr(param));
        JSONObject jsonObject = JSONUtil.parseObj(json);
        if (jsonObject.getInt("errcode")!=0){
            log.error("获取手机号失败",jsonObject.getStr("errmsg"));
            throw new BaseException("获取手机号失败",jsonObject.getStr("errmsg"));
        }

        return jsonObject.getJSONObject("phone_info").getStr("phoneNumber");
    }

    private String getToken() {
        Map<String, Object> param = getAppConfig();
        String result = HttpUtil.get(TOKEN_URL, param);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if (ObjectUtil.isNotNull(jsonObject.getInt("errcode"))){
            throw new RuntimeException(jsonObject.getStr("errmsg"));
        }
        return jsonObject.getStr("access_token");


    }

    /**
     * 封装公共参数
     * @return
     */
    private Map<String, Object> getAppConfig() {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appId);
        paramMap.put("secret", appSecret);
        return paramMap;
    }
}
